<?php

/**
 * ECSHOP 公用函数库
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: lib_common.php 17217 2011-01-19 06:29:08Z liubo $
 */

if (!defined('IN_ECS'))
{
    die('Hacking attempt');
}

/**
 * 返回给定文章分类的子栏目
 * @param $parent_id
 * @return mixed
 */
function get_article_cat($parent_id)
{
    $sql = 'SELECT cat_id,cat_name,out_url FROM '.$GLOBALS['ecs']->table('article_cat').' WHERE 1=1 ';
    if($parent_id)
    {
        $sql.= ' AND parent_id = '.$parent_id;
    }
    $rows = $GLOBALS['db']->GetAll($sql);
    foreach($rows as $k => $v){
        $arr[$k]['id'] = $v['cat_id'];
        $arr[$k]['name'] = $v['cat_name'];
        $arr[$k]['url'] = empty($v['out_url'])?build_uri('article_cat',array('acid'=>$v['cat_id'])):$v['out_url'];
    }
    return $arr;
}

/**
 * 获取产品列表信息
 * @param $rectype
 * @param $cat_id
 * @param $limit
 * @return array
 */
function get_goods_list($rectype,$cat_id,$limit,$call_child=false)
{
    if($rectype=='hot'){
        $addsql = ' is_hot = 1 ';
    }
    elseif($rectype=='new'){
        $addsql = ' is_new = 1 ';
    }
    elseif($rectype=='best'){
        $addsql = ' is_best = 1 ';
    }


    if($call_child){
        $cats = cat_list(0,$cat_id,false);
        $catstr = '';
        foreach($cats as $k => $v){
            $catstr .= $v['cat_id'] .',';
        }
        $catstr = substr($catstr,0,strripos($catstr,","));
        $sql = "SELECT * FROM ".$GLOBALS['ecs']->table("goods")." WHERE cat_id =".$cat_id." OR cat_id IN (".$catstr.") AND ".$addsql." LIMIT 0,".$limit;
    }
    else{
        $sql = "SELECT * FROM ".$GLOBALS['ecs']->table("goods")." WHERE cat_id =".$cat_id." AND ".$addsql." LIMIT 0,".$limit;
    }

    $res = $GLOBALS['db']->query($sql);
    while($dbrows = $GLOBALS['db']->fetch_array($res))
    {
        $rows[] =$dbrows;
    }
    foreach($rows as $key => $val){
        $arr['goods'][$rows[$key]['goods_id']]['goods_id'] = $val['goods_id'];
        $arr['goods'][$rows[$key]['goods_id']]['goods_name'] = $val['goods_name'];
        $arr['goods'][$rows[$key]['goods_id']]['goods_thumb'] = $val['goods_thumb'];
        $arr['goods'][$rows[$key]['goods_id']]['market_price'] = $val['market_price'];
        $arr['goods'][$rows[$key]['goods_id']]['shop_price'] = $val['shop_price'];
        $arr['goods'][$rows[$key]['goods_id']]['premote_price'] = $val['premote_price'];
        $arr['goods'][$rows[$key]['goods_id']]['goods_url'] = build_uri('goods',array('gid'=>$val['goods_id']));
    }
    $arr['cat_url'] = build_uri('category',array('cid'=>$cat_id));
    return $arr;
}

/**
 * 获取指定栏目及类型的文章列表信息
 * @param $cat_id
 * @param $article_type
 * @param int $start
 * @param int $limit
 * @return mixed
 */
function get_articles($cat_id,$article_type,$start=0,$limit=10){
    $sql = 'SELECT article_id,title,feature,score,city,motto,declaration,article_thumb,file_url FROM '.$GLOBALS['ecs']->table('article').' WHERE cat_id = '.$cat_id.' AND article_type = '.$article_type.' ORDER BY score DESC LIMIT '.$start.','.$limit.'';
    $dbrow = $GLOBALS['db']->getAll($sql);
    foreach($dbrow as $key => $val){
        $arr[$val['article_id']]['title'] = $val['title'];
        $arr[$val['article_id']]['motto'] = $val['motto'];
        $arr[$val['article_id']]['score'] = $val['score'];
        $arr[$val['article_id']]['city'] = $val['city'];
        $arr[$val['article_id']]['file_url'] = $val['file_url'];
        $arr[$val['article_id']]['declaration'] = $val['declaration'];
        $arr[$val['article_id']]['article_thumb'] = $val['article_thumb'];
        $arr[$val['article_id']]['article_url'] = build_uri('article',array('aid'=>$val['article_id']));
    }
    return $arr;
}
?>